package cn.team11.smarteye.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.team11.sds.dayi.R;

import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.team11.smarteye.base.BaseActivity;
import cn.team11.smarteye.bean.CateyeEvent;
import cn.team11.smarteye.contract.InComingCallContract;
import cn.team11.smarteye.impl.InComingCallPresenterImpl;
import cn.team11.smarteye.misc.SConstants;
import cn.team11.smarteye.utils.AppLogger;

/**
 * Created by Murphy on 2017/11/7.
 */

public class InComingActiviity extends BaseActivity<InComingCallContract.Presenter> implements InComingCallContract.View{

    @BindView(R.id.nick_name_tv)
    TextView mNickTv;
    @BindView(R.id.ring_call_preview)
    ImageView mRingCallPreview;

    private String incomingBid; //设备唯一值： bid
    private String incomingUid; //设备通信id： uid
    private String incomingName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_incoming_layout);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initData();
    }

    @Override
    public void setPresenter(InComingCallContract.Presenter presenter) {
        this.mBasePresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void init() {

        initData();

        //初始化presenter
        new InComingCallPresenterImpl(this);

        if (mBasePresenter != null){
            mBasePresenter.playRing();
        }
    }

    private void initData(){
        //获取参数
        incomingBid = getIntent().getStringExtra(SConstants.BID);
        incomingUid = getIntent().getStringExtra(SConstants.UID);
        incomingName = getIntent().getStringExtra(SConstants.NAME);

        //显示昵称
        String nick = getIntent().getStringExtra(SConstants.NICKNAME);
        if (nick == null || StringUtils.isEmpty(nick)){
            mNickTv.setText(getResources().getString(R.string.smart_cateye));
        } else {
            mNickTv.setText(nick);
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onLoginResult(boolean result) {

    }

    @Override
    public void onNetAvailable(boolean isAvailable) {

    }

    @Override
    public void onCateyeServiceBinded() {

    }


    @OnClick({R.id.incoming_hang_up, R.id.audio_answer_bt, R.id.video_answer_bt})
    public void onClickAction(View view){
        Intent videoCallIntent = new Intent(this, VideoCallActivity.class);
        videoCallIntent.putExtra(SConstants.UID, incomingUid);
        videoCallIntent.putExtra(SConstants.FROM_INCOMING, true);
        videoCallIntent.putExtra(SConstants.NAME, incomingName);
        switch (view.getId()){
            case R.id.incoming_hang_up:
                sendCloseBroadcast();
                break;
            case R.id.audio_answer_bt:
                videoCallIntent.putExtra(SConstants.IS_AUDIO_ONLY, true);
                startActivity(videoCallIntent);
                break;
            case R.id.video_answer_bt:
                videoCallIntent.putExtra(SConstants.IS_AUDIO_ONLY, false);
                startActivity(videoCallIntent);
                break;
        }
        if (mBasePresenter != null) mBasePresenter.stopRing();
        finish();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onIncomingCall(CateyeEvent.IncomingCallPreviewEvent event){
        //门铃按下时会抓拍一张图片
        String fid = event.getFid();
        if (mBasePresenter != null){
            mBasePresenter.loadDoorPreviewImage(fid, incomingBid);
        }
    }

    @Override
    public ImageView getPreviewImageView() {
        return mRingCallPreview;
    }


    @Subscribe (threadMode = ThreadMode.MAIN)
    public void onPhoneStateListener(CateyeEvent.PhoneCallEvent event){
        if (event.getState() == TelephonyManager.CALL_STATE_RINGING){//来电
            if (mBasePresenter != null){
                mBasePresenter.stopRing();
                sendCloseBroadcast();
                finish();
            }
        }
    }

    @Subscribe (threadMode = ThreadMode.MAIN)
    public void onAnotherCallEvent(CateyeEvent.CloseCurrentCallEvent event){
        if (mBasePresenter != null){
            mBasePresenter.stopRing();
            sendCloseBroadcast();
            finishAll();
        }
    }

    private void sendCloseBroadcast(){
        Intent intent = new Intent();
        intent.setAction(SConstants.INCOMING_CLOSE_ACTION);
        sendBroadcast(intent);
    }
}
